
module.exports = function ($scope, $state, $stateParams,$resource,$modal,FileUploader, $modalInstance, toaster, items) {
  console.log('编辑项目');
  $scope.notice = {
    'info' : {}
  }
  $scope.item = items;
  $scope.a = {
    'project_name' : '',
    'project_file' : '',
    'recommend_type': '', // 责任处室
    'project_type' : '', //项目类别(公告类型)
    'city_project' : '', // 是否为本市项目
    'company_code_name' : ''
  };
  // 详情
  $scope.getInfo = function(){
      $resource('/api/as/ggc/reviewPlanning/findInfo', {}, {}).save({'project_code':items.project_code}, function(res){			              
          if(res.errcode === 0 || res.errcode === 10003){
              console.log(res);
              $scope.a = res.data;
              if($scope.a.city_project == '0'){
                  $scope.a.city_project_name = '区县项目'
              } else if($scope.a.city_project == '1'){
                  $scope.a.city_project_name = '市本级项目'
              } 
              if($scope.a.project_build == '0'){
                  $scope.a.project_build_name = '建设类'
              } else if($scope.a.project_build == '1'){
                  $scope.a.project_build_name = '非建设类'
              }
              if($scope.a.project_level=='3'){
                  $scope.a.project_level_name='国家级'
              } else if($scope.a.project_level=='1'){
                  $scope.a.project_level_name='省级'
              } else if($scope.a.project_level=='2'){
                  $scope.a.project_level_name='市级'
              } else if($scope.a.project_level=='4'){
                  $scope.a.project_level_name='区域级'
              } 
              // 获取公告类型
              $scope.getGuideType($scope.a.project_type);
              // 材料
              console.log('材料');
              console.log(JSON.parse($scope.a.project_file));
              $scope.material = JSON.parse($scope.a.project_file);
              if($scope.material.aa){
                $scope.array1 = $scope.material.aa;
                dic.aa = $scope.array1;
              } 
              if($scope.material.bb){
                $scope.array2 = $scope.material.bb;
                dic.bb = $scope.array2;
              } 
              $scope.array3 = $scope.material.cc;
              dic.cc = $scope.array3;
              $scope.array4 = $scope.material.dd;
              dic.dd = $scope.array4;
              $scope.array5 = $scope.material.ee;
              dic.ee = $scope.array5;
              $scope.array6 = $scope.material.ff;
              dic.ff = $scope.array6;
              $scope.array7 = $scope.material.gg;
              dic.gg = $scope.array7;
              $scope.array8 = $scope.material.hh;
              dic.hh = $scope.array8;
              $scope.array9 = $scope.material.ii;
              dic.ii = $scope.array9;
          }else{
              alert(res.errmsg);
          }
      });
  }
  $scope.getInfo();
  // 获取公告名称列表
  $scope.getNoticeList = function(){
      $resource('/api/as/ggc/reviewGuide/findCompanysList', {}, {}).save({}, function(res){
          if(res.errcode != 0){ 
              toaster.error({title:"",body:res.errmsg});
              return;
          }
          console.log(res);
          $scope.noticeArr = res.data;
      });
  }
  // 所属区县
  $resource('/api/as/ggc/reviewCompany/getCompanyCity', {}, {}).save({}, function(res){
      if(res.errcode != 0){ 
          toaster.error({title:"",body:res.errmsg});
          return;
      }
      console.log(res);
      $scope.a.company_code_name = res.data.company_code_name;
  });
  $scope.getNoticeList();
  $scope.change = function(aa){
    console.log(aa);
    $scope.a.project_type = aa.guide_type;
    $scope.a.city_project = aa.city_project;
    $scope.a.recommend_type = aa.recommend_type;
    $scope.a.project_build = aa.project_build;
    $scope.a.project_level = aa.project_level;
    $scope.getRecommendName($scope.a.recommend_type);
    $scope.getGuideType(aa.guide_type);
    if(aa.project_build == '1'){
      $scope.a.project_build_name = '非建设类';
    } else if(aa.project_build == '0') {
      $scope.a.project_build_name = '建设类';
    }
    if(aa.project_level == '1'){
      $scope.a.project_level_name = '市级';
    } else if(aa.project_level == '2') {
      $scope.a.project_level_name = '省级';
    } else if(aa.project_level == '3') {
      $scope.a.project_level_name = '国家级';
    } else if(aa.project_level == '4') {
      $scope.a.project_level_name = '区域级';
    }
  }
  // 责任处室
  $scope.officeFileArr = [
    {name:'规划发展处',value:'3', id : ['8', '9']},
    {name:'行业管理处  ',value:'6', id : ['3', '4']},
    {name:'人事编制处 ',value:'1', id : ['1', '2']},
    {name:'政策法规处 ',value:'2', id : ['3', '4']},
    {name:'区域合作处',value:'4', id : ['5', '6', '7']},
    {name:'市场开发处 ',value:'5', id : ['1', '2']},
    {name:'产业发展处',value:'7', id : ['8', '9']},
    {name:'特色旅游处',value:'8', id : ['5', '6', '7']},
    {name:'旅游质量监督管理所',value:'9', id : ['3', '4']},
    {name:'旅游产业发展服务中心',value:'10', id : ['8', '9']},
    {name:'机关党委',value:'11', id : ['5', '6', '7']},
    {name:'办公室',value:'12', id : ['5', '6', '7']}
  ]
  $scope.getRecommendName = function(recommend_type){
    for(var i = 0; i < $scope.officeFileArr.length; i++){
      var array = $scope.officeFileArr[i].id;
      array.forEach(element => {
        if(recommend_type == element){
          $scope.a.recommend_type_name = $scope.officeFileArr[i].name;
          console.log($scope.officeFileArr[i].name);
        }
      });
    }
  }
  // 公告类型
  $scope.typeFileArr = [
      {name:'王者类型',value:'1', id : '1'},
      {name:'青铜',value:'2', id : '2'},
      {name:'白银',value:'3', id : '3'},
      {name:'王者类型1',value:'4', id : '4'},
      {name:'青铜1',value:'5', id : '5'},
      {name:'白银1',value:'6', id : '6'},
      {name:'王者类型2',value:'7', id : '7'},
      {name:'青铜2',value:'8', id : '8'},
      {name:'白银2',value:'9', id : '9'}        
  ]
  $scope.getGuideType = function(guide_type){
      $scope.typeFileArr.forEach(element => {
        if(guide_type == element.value){
          $scope.a.project_type_name = element.name;
        }
      });
  }
  $scope.del=function(index, number, str){
      if(number == '1'){
        $scope.array1.splice(index, 1);
        dic.aa = $scope.array1;
      }
      if(number == '2'){
        $scope.array2.splice(index, 1);
        dic.bb = $scope.array2;
      }
      if(number == '3'){
        $scope.array3.splice(index, 1);
        dic.cc = $scope.array3;
      }
      if(number == '4'){
        $scope.array4.splice(index, 1);
        dic.dd = $scope.array4;
      }
      if(number == '5'){
        $scope.array5.splice(index, 1);
        dic.ee = $scope.array5;
      }
      if(number == '6'){
        $scope.array6.splice(index, 1);
        dic.ff = $scope.array6;
      }
      if(number == '7'){
        $scope.array7.splice(index, 1);
        dic.gg = $scope.array7;
      }
      if(number == '8'){
        $scope.array8.splice(index, 1);
        dic.hh = $scope.array8;
      }
      if(number == '9'){
        $scope.array9.splice(index, 1);
        dic.ii = $scope.array9;
      }
      console.log(dic);
  }
  $scope.arr = [];
  var dic = {};
  $scope.image = function(aa){
    var para = $state.get('app.imageupload');
    //设置上传文件夹，以自己业务命名
    angular.extend(para, {
      resolve : {  
        'dir' : function(){
          return 't1';
        }
      } 
    })
    var modalInstance = $modal.open(para);
    modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数  
      console.log('modal is opened');  
    });  
    modalInstance.result.then(function(result) {  
      if(aa == '1'){ // 材料1
        $scope.array1 = result;
        dic.aa = $scope.array1;
        console.log(dic);
      } else if(aa == '2'){
        $scope.array2 = result;
        dic.bb = $scope.array2;
        console.log(dic);
      } else if(aa == '3'){
        $scope.array3 = result;
        dic.cc = $scope.array3;
        console.log(dic);
      } else if(aa == '4'){
        $scope.array4 = result;
        dic.dd = $scope.array4;
        console.log(dic);
      } else if(aa == '5'){
        $scope.array5 = result;
        dic.ee = $scope.array5;
        console.log(dic);
      } else if(aa == '6'){
        $scope.array6 = result;
        dic.ff = $scope.array6;
        console.log(dic);
      } else if(aa == '7'){
        $scope.array7 = result;
        dic.gg = $scope.array7;
        console.log(dic);
      } else if(aa == '8'){
        $scope.array8 = result;
        dic.hh = $scope.array8;
        console.log(dic);
      } else if(aa == '9'){
        $scope.array9 = result;
        dic.ii = $scope.array9;
        console.log(dic);
      }
      // $scope.a.project_file = $scope.array.join(',');
    }, function(reason) {  
      console.log(reason);// 点击空白区域，总会输出backdrop  
      // click，点击取消，则会暑促cancel  
      $log.info('Modal dismissed at: ' + new Date());  
    }); 
  };
  // 提交
  $scope.submit = function(){
    console.log($scope.a);
    $scope.a.project_file = JSON.stringify(dic);
      if($scope.a.project_name==null||$scope.a.project_name==''||$scope.a.project_name==undefined){
          alert('请填写项目名称');
          return;
      }
      if($scope.a.project_file==null||$scope.a.project_file==''||$scope.a.project_file==undefined){
          alert('请上报材料');
          return;
      }
      $resource('/api/ac/ggc/reviewCompanyProjectService/createCompanyProject', {}, {}).save($scope.a, function(res){
          if(res.errcode === 0){ 
              toaster.success({title:"",body:'操作成功'});
              $modalInstance.close();
              // $state.go('app.project_search', {'type' : '0'});
          }else{
              toaster.error({title:"",body:res.errmsg});
          }
      });
    }
  
};
